package com.example.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.entity.KnowledgeElement;
import com.example.entity.Result;
import com.example.service.KnowledgeElementService;
import com.example.utils.ResultUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 *
 */
@RestController
@RequestMapping("/knowledgeElement")
public class KnowledgeElementController {
    @Autowired
    KnowledgeElementService knowledgeElementService;

    @GetMapping("/selectOne")
    public Result selectOne(Long id, HttpServletResponse response){
        KnowledgeElement knowledgeElementNode = knowledgeElementService.selectById(id);
        response.setHeader("Access-Control-Allow-Origin", "*");
        return ResultUtil.success(knowledgeElementNode);
    }

    @PostMapping("/updateContent")
    public Result updateContent(@RequestBody  KnowledgeElement knowledgeElement) {
        KnowledgeElement knowledgeElementNode = knowledgeElementService.updateContent(knowledgeElement);
        return ResultUtil.success(knowledgeElementNode);
    }


    @PostMapping("/update")
    public Result update(@RequestBody  KnowledgeElement knowledgeElement) {
        JSONObject jsonObject = knowledgeElementService.update(knowledgeElement);
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            KnowledgeElement knowledgeElementNode = (KnowledgeElement)jsonObject.get("data");
            return ResultUtil.success(knowledgeElementNode);
        }
        return ResultUtil.error(500, msg);
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        JSONObject jsonObject = knowledgeElementService.selectAll();
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            List<KnowledgeElement> knowledgeElementNodeList = (List<KnowledgeElement>) jsonObject.get("data");
            return ResultUtil.success(knowledgeElementNodeList);
        }
        return ResultUtil.error(500, msg);
    }

    @PostMapping("/insert")
    public Result insert(@RequestBody KnowledgeElement knowledgeElement) {
        JSONObject jsonObject = knowledgeElementService.insert(knowledgeElement);
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            KnowledgeElement knowledgeElementNode = (KnowledgeElement) jsonObject.get("data");
            return ResultUtil.success(knowledgeElementNode);
        }
        return ResultUtil.error(500, msg);
    }

    @PostMapping("/deleteById")
    public Result deleteById(@RequestBody KnowledgeElement knowledgeElement) {
        JSONObject jsonObject = knowledgeElementService.deleteById(knowledgeElement.getId());
        String msg = (String)jsonObject.get("msg");
        if (msg.equals("success")) {
            KnowledgeElement knowledgeElementNode = (KnowledgeElement) jsonObject.get("data");
            return ResultUtil.success(knowledgeElementNode);
        }
        return ResultUtil.error(500, msg);
    }
}
